
--[[

    slimui - Copyright (c) 2011 atom0s, All rights reserved.

    See Copyright notice in slimui.lua
    
]]--

----------------------------------------------------------------------------------
-- func: table.deepcopy
-- desc: Creates a deep copy of the given table.
-- auth: < Unknown; modified by Wiccaan >
----------------------------------------------------------------------------------
function table.deepcopy( t )
    if type( t ) ~= 'table' then return t; end
    
    local t_mt = getmetatable( t );
    local deep_copy = { };
    
    for k, v in pairs( t ) do
        if type( v ) == 'table' then
            v = table.deepcopy( v );
        end
        
        deep_copy[ k ] = v;
    end
    
    setmetatable( deep_copy, t_mt );
    return deep_copy;
end

----------------------------------------------------------------------------------
-- func: table.dump
-- desc: Returns the given table dumped into a string.
-- auth: < sdonovan; modified by Wiccaan >
----------------------------------------------------------------------------------
function table.dump( t )
    if type( t ) ~= 'table' then return tostring( t ); end
    
    local str = '{ ';
    
    for k, v in pairs( t ) do
        if type( k ) ~= 'number' then k = '"' .. k .. '"' end
        str = str .. '[' .. k .. '] = ' .. table.dump( v ) .. ', ';
    end
    
    return str .. '} ';
end

----------------------------------------------------------------------------------
-- func: table.hasValue
-- desc: Returns whether the table contains a value or not.
----------------------------------------------------------------------------------
function table.hasValue( t, val )
    if type( t ) ~= 'table' then return false; end
    
    for k, v in pairs( t ) do
        if v == val then return true; end
    end
    
    return false;
end
